<?php

namespace app\admin\controller;

use app\service\PayService;
use app\model\Config;
use think\facade\Cache;
use think\Controller;
use think\Request;

class ProductOrder extends Controller
{
    protected $middleware = [
        'CheckAdmin'
    ];

    public function lists(Request $request)
    {
        if ($request->isAjax()) {
            $page = $request->get('page', 1, 'intval');
            $page = $page > 0 ? $page : 1;
            $limit = $request->get('limit', 10, 'intval');
            $name = $request->get('name', '', 'trim');
            $status = $request->get('status', 0, 'intval');
            $transaction = $request->get('transaction', '', 'trim');
            $product_order = Db('productOrder');
            if ($name != '') {
                $product_ids = Db('product')->where('name', 'like', '%' . $name . '%')->column('product_id');
                $product_ids[] = 0;
                $product_order->where('product_id', 'in', $product_ids);
            }
            if ($transaction != '') {
                $product_order->where('transaction', $transaction);
            }
            if ($status != 0) {
                $product_order->where('status', $status - 2);
            }
            $count = $product_order->count();
            $list = $product_order
                ->page($page, $limit)
                ->order('create_time desc')
                ->select();
            return ['code' => 0, 'data' => $list, 'count' => $count];
        } else {
            return $this->fetch();
        }
    }

    public function editOrder(Request $request)
    {
        if ($request->isPost()) {
            $data = [];
            $order_id = $request->post('order_id', 0, 'intval');
            $status = $request->post('status', 0, 'intval');
            $info = \app\model\ProductOrder::where('order_id', $order_id)->find()->toArray();
            if (\app\model\ProductOrder::setStatus($order_id, $status)) {
                $data['description'] = $request->post('description', '', 'trim');
                if (false === Db('productOrder')->where('order_id', $order_id)->update($data)) {
                    return ['code' => 1, 'msg' => '操作失败'];
                } else {
                    if ($status == \app\model\ProductOrder::PAY_REFUNDING && $info['status'] == \app\model\ProductOrder::PAY_SUCCESS) {
                        $weixinpay = new PayService('wechat');
                        $info['notify'] = Cache::remember('app_host', Config::getConfigValue('app_host')) . '/api/user/productRefundNotify';
                        $return = $weixinpay->refund($info);
                        if ($return['order']) {
                            \app\model\ProductOrder::where('order_id', $order_id)->setField('refund_id', $return['refund_id']);
                        }else{
                            \app\model\ProductOrder::where('order_id', $order_id)->setField('status', $info['status']);
                            return ['code' => 1, 'msg' => $return['msg']];
                        }
                    }
                    return ['code' => 0, 'msg' => '操作成功'];
                }
            } else {
                return ['code' => 1, 'msg' => '操作失败'];
            }
        } else {
            $order_id = $request->get('id', 0, 'intval');
            $info = Db('productOrder')
                ->alias('o')
                ->join('user u', 'o.user_id = u.user_id', 'LEFT')
                ->where('o.order_id', $order_id)
                ->field('o.*,u.nickname,u.avatar,u.company,u.name,u.sex,u.position,u.phone,u.address')
                ->find();
            $this->assign('info', $info);
            return $this->fetch();
        }
    }
}